package com.zjht.gmcc.bean.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * @author ycl 2010-06-04
 *  月份下拉框控件
 */
public class SelectMonth extends TagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6763941318192220678L;
	private String name;//控件name属性，默认为month
	private String id;//控件ID，默认为month
	private int selected;//选中值
	private String onchange;//事件
	private String style;//样式,如display:none
	private int totalMonth;//可显示的总月数，当前年截止到上月，默认为12
	
	public String getStyle() {
		return style;
	}
	public void setStyle(String style) {
		this.style = style;
	}
	public int getTotalMonth() {
		return totalMonth;
	}
	public void setTotalMonth(int totalMonth) {
		this.totalMonth = totalMonth;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getOnchange() {
		return onchange;
	}
	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}
	
	public int getSelected() {
		return selected;
	}
	public void setSelected(int selected) {
		this.selected = selected;
	}
	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {
		
		String name = this.name;
		String id = this.id;
		int selected = this.selected;
		String onchange = this.onchange;
		String style = this.style;
		int totalMonth = this.totalMonth;		
		
		if(name==null||name.length()==0)
			name="month";
		if(id==null||id.length()==0)
			id=name;
		StringBuilder html=new StringBuilder();
		html.append("<select name=\"");
		html.append(name);
		html.append("\" ");
		html.append("id=\"");
		html.append(id);
		html.append("\" ");
		if(onchange!=null && onchange.length()>0){
			html.append("onchange=\"");
			html.append(onchange);
			html.append("\" ");
		}
		if(style!=null && style.length()>0){
			html.append("style=\"");
			html.append(style);
			html.append("\" ");
		}
		html.append(">");
		if(totalMonth==0)totalMonth=12;
		for(int i=1;i<=totalMonth;i++){
			html.append("<option value=\"");
			html.append(i);
			html.append("\" ");
			if(selected==i)
				html.append("selected");
			html.append(">");
			html.append(i+"月");
			html.append("</option>");
			
		}
		html.append("</select>");
		try {
			pageContext.getOut().print(html.toString());
		} catch (IOException e) {
			throw new JspException(e.getMessage());
		}
		html=null;
		return this.SKIP_BODY;
	}
}
